Lumen & Gesso / website · lumengesso.com / app / api / orders / [id] / approve / route.ts
| 1 | import { NextResponse, type NextRequest } from 'next/server'; |
| 2 | import { getCustomerUser } from '@/app/lib/lumen-auth'; |
| 3 | import { commerceDb } from '@/app/lib/commerce-db'; |
| 4 | import { sendEmail } from '@/app/lib/email'; |
| 5 | import { env } from '@/app/lib/env'; |
| 6 | import { displayOrderId } from '@/app/lib/orders'; |
| 7 | import { customerApprovedAdminNotificationHtml } from '@/app/lib/order-emails'; |
| 8 | |
| 9 | export const runtime = 'nodejs'; |
| 10 | export const dynamic = 'force-dynamic'; |
| 11 | |
| 12 | // Customer approves the previewed portrait. Payment was collected |
| 13 | // up-front at /checkout, so no Stripe charge happens here — we just |
| 14 | // transition awaiting_approval → approved and notify the studio. The |
| 15 | // admin then does one final review and clicks "Send to printer" in |
| 16 | // the admin panel, which creates the Prodigi order. Print never |
| 17 | // fires automatically from a customer click — admin always has a |
| 18 | // final go/no-go. |
| 19 | export async function POST( |
| 20 | req: NextRequest, |
| 21 | ctx: { params: Promise<{ id: string }> }, |
| 22 | ) { |
| 23 | const { id } = await ctx.params; |
| 24 | |
| 25 | const user = await getCustomerUser(); |
| 26 | if (!user) return NextResponse.json({ error: 'unauthorized' }, { status: 401 }); |
| 27 | |
| 28 | let body: { termsAccepted?: boolean; generationId?: unknown } | null; |
| 29 | try { |
| 30 | body = await req.json(); |
| 31 | } catch { |
| 32 | return NextResponse.json({ error: 'invalid json' }, { status: 400 }); |
| 33 | } |
| 34 | if (body?.termsAccepted !== true) { |
| 35 | return NextResponse.json({ error: 'You must accept the no-refund terms to approve.' }, { status: 400 }); |
| 36 | } |
| 37 | |
| 38 | const admin = commerceDb(); |
| 39 | const { data: order } = await admin |
| 40 | .from('orders') |
| 41 | .select('id, user_id, user_email, status, price_cents, current_generation_id') |
| 42 | .eq('id', id) |
| 43 | .single(); |
| 44 | |
| 45 | if (!order) return NextResponse.json({ error: 'order not found' }, { status: 404 }); |
| 46 | if (order.user_id !== user.id) return NextResponse.json({ error: 'forbidden' }, { status: 403 }); |
| 47 | const generationId = body.generationId; |
| 48 | if (typeof generationId !== 'string' || generationId !== order.current_generation_id) { |
| 49 | return NextResponse.json({ error: 'The proof has changed. Reload to review the current proof.' }, { status: 409 }); |
| 50 | } |
| 51 | if (order.status === 'approved') return NextResponse.json({ ok: true, idempotent: true }); |
| 52 | if (order.status !== 'awaiting_approval') { |
| 53 | return NextResponse.json({ error: 'This proof is no longer open for approval. Reload to see the latest status.' }, { status: 409 }); |
| 54 | } |
| 55 | |
| 56 | const { data: claimed, error: updErr } = await admin.from('orders').update({ |
| 57 | status: 'approved', |
| 58 | terms_accepted_at: new Date().toISOString(), |
| 59 | }).eq('id', id).eq('status', 'awaiting_approval').eq('current_generation_id', generationId).select('id'); |
| 60 | if (updErr) { |
| 61 | return NextResponse.json({ error: `update failed: ${updErr.message}` }, { status: 500 }); |
| 62 | } |
| 63 | if (!claimed || claimed.length === 0) { |
| 64 | return NextResponse.json({ error: 'The proof changed while you were approving it. Reload the order to continue.' }, { status: 409 }); |
| 65 | } |
| 66 | |
| 67 | // Notify the studio so admin knows it's time to do a final review |
| 68 | // and send to the printer. Best-effort — don't fail the approve on |
| 69 | // email errors. |
| 70 | await sendEmail({ |
| 71 | to: env.site.studioEmail(), |
| 72 | subject: `Approved & awaiting print · ${displayOrderId(order.id)} — ${order.user_email}`, |
| 73 | html: customerApprovedAdminNotificationHtml(order.id, order.user_email, order.price_cents), |
| 74 | text: `${order.user_email} approved order ${order.id}. Review in admin and send to printer: ${env.site.url()}/admin/orders/${order.id}`, |
| 75 | }).catch(() => {}); |
| 76 | |
| 77 | return NextResponse.json({ ok: true }); |
| 78 | } |